<?php

/**
 * login actions.
 *
 * @package    dirsac
 * @subpackage login
 * @author     Miguel Flores
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->form = new LoginForm();
  }
  
  public function executeLogin(sfWebRequest $request)
  {
    if ($request->isMethod('post')) {
      
      $user = Doctrine::getTable('Usuario')->findOneByLogin($request->getParameter('login'));
      //$credenciales = Usuario::getPermisos($user->getId());
      
      if ($user == '') {
        $this->getUser()->setFlash('no_user', 'User not found');
        $this->redirect('login/index');
      }
      
      $pass = md5($user->getSalt().$request->getParameter('password'));
      
      if ($user->getPassword() != $pass) {
        $this->getUser()->setFlash('no_pass', 'Incorrect Password');
        $this->redirect('login/index');
      }
      
      if ($user->getPassword() == $pass) {
        $this->getUser()->setAuthenticated(true);
        $this->getUser()->addCredential($user->getPerfil());
        $this->getUser()->setAttribute('perfil', $user->getPerfil());
        $this->getUser()->setAttribute('nombre', $user->getNombre());
        $this->redirect('login/home');
      }
				
      $this->redirect('login/index');
    }
    
  }
  
	public function executeLogout() {	
		$this->getUser()->clearCredentials();
		$this->getUser()->setAuthenticated(false);
		$this->redirect('/admin/login/index');
	}
	
	public function executeHome(sfWebRequest $request) {
		
	}

	public function executeNo(sfWebRequest $request) {
		
	}
}
